package com.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import com.model.News;
import com.service.NewsService;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.Menu;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MainActivity extends Activity {

	private ListView listView;
	private SimpleAdapter adapter;
	private List<News> newes;
	List<HashMap<String, Object>> data;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		listView = (ListView) this.findViewById(R.id.listView);
		final String length = this.getResources().getString(R.string.length);
		data = new ArrayList<HashMap<String, Object>>();
		adapter = new SimpleAdapter(this, data, R.layout.item, new String[] {
				"title", "timelength" }, new int[] { R.id.title,
				R.id.timelength });
		Handler handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				if (msg.what == 0x234) {
					newes = (List<News>) msg.obj;
					for (News news : newes) {
						HashMap<String, Object> item = new HashMap<String, Object>();
						item.put("id", news.getId());
						item.put("title", news.getTitle());
						item.put("timelength", length + news.getTimelength());
						data.add(item);
					}
					listView.setAdapter(adapter);
				}
			}
		};
		new Thread(new NewsService(handler)).start();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
